package com.sinaapp.kjWeb.Schedule;

import java.util.Timer;
import java.util.TimerTask;

import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.sinaapp.kjWeb.action.Action;
import com.sinaapp.kjWeb.action.CheckInAction;
import com.sinaapp.kjWeb.action.CheckOutAction;
import com.sinaapp.kjWeb.action.LoginAction;
import com.sinaapp.kjWeb.util.ConfigUtils;
import com.sinaapp.kjWeb.util.DateUtil;
import com.sinaapp.kjWeb.util.StringUtil;


/*
 *auth:徐疆
 *date 2014-1-12 ����7:29:29
 */
public class KJSchedule extends Schedule{
	public static Logger logger=LoggerFactory.getLogger(KJSchedule.class);
	BasicCookieStore cookieStore = new BasicCookieStore();
	CloseableHttpClient httpclient = HttpClients.custom()
			.setDefaultCookieStore(cookieStore).build();
	private Timer KJtimer = new Timer();
	class KJControl  extends TimerTask{
		Action action;
		public KJControl(Action s){
			action=s;
		}
		@Override
		public void run() {
			try{
				action.setHttpClient(httpclient);
				cookieStore.clear();
				action.mainStart();
			}catch(Exception e){
				e.printStackTrace();
				logger.error("",e);
			}finally{
				
			}
		}
	}
	
	public void addSchedule(){
		String checkInTime=ConfigUtils.getInstance().getValue("kj_checkInTime");
		String checkOutTime=ConfigUtils.getInstance().getValue("kj_checkOutTime");
		if(StringUtil.isNotBlank(checkInTime)){
			Action loginAction=new LoginAction();
			loginAction.setNextAction(new CheckInAction());
			KJtimer.schedule(new KJControl(loginAction), DateUtil.getDateFromTime(checkInTime),24*60*60*1000);
		}
		if(StringUtil.isNotBlank(checkOutTime)){
			Action loginAction=new LoginAction();
			loginAction.setNextAction(new CheckOutAction());
			KJtimer.schedule(new KJControl(loginAction), DateUtil.getDateFromTime(checkOutTime),24*60*60*1000);
		}
	}
}

